#coding=utf8
from __future__ import division
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.autograd import Variable

class loose_r_loss(nn.Module):
    def __init__(self, r=40, weight=0.005):
        super(loose_r_loss, self).__init__()
        self.r = r
        self.weight = weight
    def forward(self,  emd):
        '''
        :param y_preds: (N, f), Variable of FloatTensor, embedding features
        :return:
        '''

        return self.weight * 0.5 * torch.mean((torch.norm(emd, p=2,dim=1)-self.r)*(torch.norm(emd, p=2,dim=1)-self.r))
